package controller

import (
	"github.com/gin-gonic/gin"
	"web/app"
	"web/app/dao"
	"web/app/mode"
	"xutil/xStr"
)

type c_key struct {
	dao dao.CDK
}

func (c2 c_key) cread(c *gin.Context) {
	var k mode.CDK
	err := c.ShouldBind(&k)
	if err != nil {
		app.Response(c, 400, "参数异常")
		return
	}
	num := xStr.Str2int(c.PostForm("num"))
	if num == 0 {
		num = 100
	} else if num > 1000 {
		num = 1000
	}

	err, list := c2.dao.Cread(num, &k)
	if err != nil {
		app.Response(c, 400, "生成失败")
		return
	}
	app.Response_data(c, 200, "生成成功", list)
}

func (c2 c_key) getAll(c *gin.Context) {
	var k mode.CDK
	err := c.ShouldBind(&k)
	if err != nil {
		app.Response(c, 400, "参数异常")
		return
	}
	err, list := c2.dao.GetAll(&k)
	if err != nil {
		app.Response(c, 400, "查询失败")
		return
	}
	app.Response_list(c, 200, "查询成功", list)
}

func (c2 c_key) edit(c *gin.Context) {
	var k mode.CDK
	err := c.ShouldBind(&k)
	if err != nil {
		app.Response(c, 400, "参数异常")
		return
	}

	err = c2.dao.Edit(&k)
	if err != nil {
		app.Response(c, 400, "修改失败")
		return
	}
	app.Response(c, 200, "修改成功")
}

type i_key interface {
	cread(c *gin.Context)
	get(c *gin.Context)
	getAll(c *gin.Context)
	edit(c *gin.Context)
}
